package com.nightpiter.view.drawer;

import com.nightpiter.model.objects.Station;
import com.nightpiter.view.Calculator;
import com.nightpiter.view.graphics.Point;
import com.nightpiter.view.graphics.PointF;
import com.nightpiter.view.graphics.Rect;

/**
 * @author Eldar Abusalimov (eldar.abusalimov@gmail.com)
 */
public abstract class ClassicMetroDrawer extends MetroDrawer {

	/** computed bounds of the model by position values */
	private Rect bounds;

	@Override
	protected boolean internalPrepare() {
		float minX, minY, maxX, maxY;
		float x, y;
		minX = Float.POSITIVE_INFINITY;
		minY = Float.POSITIVE_INFINITY;
		maxX = Float.NEGATIVE_INFINITY;
		maxY = Float.NEGATIVE_INFINITY;

		PointF coordF = new PointF();
		for (Station station : modelData.getStations()) {
			coordF.set(station, mode);
			x = coordF.x;
			y = coordF.y;
			if (x < minX) {
				minX = x;
			}
			if (y < minY) {
				minY = y;
			}
			if (x > maxX) {
				maxX = x;
			}
			if (y > maxY) {
				maxY = y;
			}
		}

		this.bounds = new Rect((int) minX, (int) minY, (int) maxX, (int) maxY);

		return super.internalPrepare();
	}

	@Override
	public Rect getBounds() {
		return new Rect(this.bounds);
	}

	protected Station classicSearchStation(Point point, Calculator calculator) {
		final int DELTA = 7;
		float minDist = Float.POSITIVE_INFINITY;
		float dist = 0;

		PointF pointF = new PointF();
		Station nearestStation = null;
		for (Station station : modelData.getStations()) {
			calculator.convert(station, pointF);
			dist = pointF.distanceTo(point);
			if (dist < minDist) {
				minDist = dist;
				nearestStation = station;
			}
		}

		if (nearestStation != null && minDist < DELTA) {
			return nearestStation;
		}
		return null;
	}

}
